package com.demo.tool;



import com.demo.exception.ServiceException;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//把普通pojo实例化到spring容器中
@Component
public class Token {

     // 创建redis对象
     @Autowired
     private StringRedisTemplate stringRedisTemplate;



    //使用uuid生成token
    public  String createToken(){
        UUID uuid=UUID.randomUUID();
        String token=uuid.toString();
        return token;
    }

    //将token存入redis
    public  void depositRedis(String username,String token){

        ValueOperations operations = stringRedisTemplate.opsForValue();
        //设置redis过期时间
        operations.set(username, token,3,TimeUnit.HOURS);
    }

    //验证token是否有效
    public Boolean verifyAndRefreshUserToken(String token){
        Boolean flag=false;
        ValueOperations operations = stringRedisTemplate.opsForValue();
        //判断token是否有效
        if(StringUtils.hasLength(token)){
            if(!JWTUTIL.verify(token)){
                throw new ServiceException("token解析失败3332");
            }
            //解析token拿到key查找token是否存在本地token
            String key=JWTUTIL.tokenInfo(token);
            if(stringRedisTemplate.hasKey(key)){
                //如果存在那就延迟一个小时
                stringRedisTemplate.expire(token,60,TimeUnit.MINUTES);
                flag=true;
            }
        }else{
            throw new ServiceException("请先登录");
        }
        return flag;
    }


    //如果token未过期重复使用
    //再次登录时 如果token还未过期 那就直接拿本地的token返回即可
    //通过当前的用户名去查找本地是否还存在token(因为存的时候就是以用户名为value)
    public String tokenIsExpired(String username){
       String token="";
        ValueOperations operations = stringRedisTemplate.opsForValue();

        //判断token是否有效
        if(StringUtils.hasLength(username)){
            if(stringRedisTemplate.hasKey(username)){
                //如果存在那就延迟30分钟
                stringRedisTemplate.expire(username,30,TimeUnit.MINUTES);
                token= (String) operations.get(username);
            }
        }else{
            throw new ServiceException("请先登录");
        }
        tokenIsExist(token);
        return token;
    }


    //查找头部token是否存在本地缓存
    public Boolean tokenIsExist(String token){
        Boolean flag=false;
        String prefix="*";

        Set<String> keys=stringRedisTemplate.keys(prefix);
        Set<String> values=new HashSet<>();
        for (String key:keys){
//            System.out.println("token"+key);
           values.add(stringRedisTemplate.opsForValue().get(key));
        }

        for(String value:values){
            if(token.equals(value)){
                flag=true;
            }
        }

        return flag;

    }


    public Long GetUserUidByToken(String token){
       return JWTUTIL.getUserUidByToken(token);
    }



}
